package com.turing.manage.listener;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
/**
 * @desc   上下文内容监听器目的是可以预加载一些数据信息
 * @author wyh
 * @time   2022-01-06
 */
import javax.servlet.ServletContextListener;

public class MyLiceneListener implements ServletContextListener {

/**
 * @desc  上下文初始化的时候触发
 */
@Override
public void contextInitialized(ServletContextEvent sce) {
	System.out.println("contextInitialized()");
	System.out.println("===============servlet服务初始化(也就是tomcat正式启动成功)==================");
    //1.立足ServletContextEvent这个事件对象，获取servletContext对象
	ServletContext servletContext = sce.getServletContext();

	//2.模拟一个连库四要素
	Map<String, Object> map_mylisternter=new HashMap<String, Object>();
	map_mylisternter.put("username", "root");
	map_mylisternter.put("password", "aizjy");
	map_mylisternter.put("url", "jdbc:mysql://localhost:3306/examonline/....");
	map_mylisternter.put("drivername", "com.mysql.jdbc.Driver");
	
	//3.将map存入到allication作用域中
	servletContext.setAttribute("map_mylisternter", map_mylisternter);
}

/**
 * @desc  上下文销毁的时候触发   
 */
@Override
public void contextDestroyed(ServletContextEvent sce) {
	System.out.println("contextDestroyed");
	System.out.println("===============servlet服务销毁(也就是tomcat重新部署)==================");
}

}
